/*
 * Copyright (C) 2010, Funcom AS, All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  @authors
 *  Svein Gunnar Larsen <sveingunnarlarsen@gmail.com>
 *  Erik Thune Lund <culex.eriklund@gmail.com>
 *  Michel Krohn-Dale <kronicz@gmail.com>
 */

package com.funcom.peeler.action;

import com.funcom.peeler.PeelStyleSheet;
import com.funcom.peeler.UpdateCont;
import com.funcom.peeler.symbol.Symbol;
import com.jmex.bui.enumeratedConstants.TextEffect;

public class StyleChange implements Change {

    PeelStyleSheet stylesheet;
    String fqstyle;
    String property;
    Object old_value;
    Object new_value;

    public StyleChange( PeelStyleSheet stylesheet, String fqstyle, String property, Object old_value, Object new_value ) {
        if ( stylesheet == null || fqstyle == null || property == null )
            throw new NullPointerException( "StyleChanges must have valid parameters!" );

        this.stylesheet = stylesheet;
        this.fqstyle = fqstyle;
        this.property = property;
        this.old_value = old_value;
        this.new_value = new_value;
    }

    public void startChange() {
    }

    public void endChange() {
    }

    public void doChange() {

        stylesheet.updateStyle( fqstyle, property, new_value );
        UpdateCont.get().invalidateAll();
    }

    public void undoChange() {

        stylesheet.updateStyle( fqstyle, property, old_value );

        if (property.equals("font")) resolveFont();

        UpdateCont.get().invalidateAll();
    }

    public boolean valid() {

        if ( old_value.equals( new_value ) ) return false;

        return true;
    }

    private void resolveFont() {
        Object value = stylesheet.properties.get( fqstyle ).properties.get( Symbol.StyleProperty.text_effect );
        Object resolveFont = TextEffect.fromInt(3);

        if ((resolveFont).equals(value)) {
            resolveFont = TextEffect.fromInt(0);
        }

        stylesheet.updateStyle( fqstyle, "text-effect", resolveFont);

        UpdateCont.get().invalidateAll();
        stylesheet.updateStyle(fqstyle, "text-effect", value);
    }
}
